<?php
    require_once($application_folder."/controllers/navigator.php");
    class Order_result extends navigator 
    {

        function __construct()
        {
            parent::__construct();
        }

        function index(){

            $this->process_result();

        }

        function process_result(){

            if(!isset($_GET['trans_id']))
                redirect(base_url());
            $trans_id = $_GET['trans_id'];
            //$trans_id = 'sKySAZUv7I1q6FT//OdT3Rvk58Y=';
            $dbRet = $this->booking->TransactionResult($trans_id);


            $trans = $this->booking->test_response($dbRet);

            if(!$trans){
                echo $this->booking->getFlag().":";

                switch($this->booking->getFlag()){
                    case 'error':
                        echo $this->booking->getError();
                        break;
                    case 'numrows':
                        echo $this->booking->getNumrows();
                        break;
                    case 'info':
                        echo $this->booking->getInfo();
                        break;

                }
                die();
            }else{
                //print_r($trans);

                $data['trans'] = $dbRet[0];
                //$dbRet[0]->result = 'OK';
                switch ($dbRet[0]->result) {

                    /*
                    * Policy Printout 
                    * 
                    */

                    case 'OK': 

                    $type = $this->booking->FindType($trans_id);
                    if(!$type){
                        echo $this->booking->getFlag().":";

                        switch($this->booking->getFlag()){
                            case 'error':
                                echo $this->booking->getError();
                                break;
                            case 'numrows':
                                echo $this->booking->getNumrows();
                                break;
                            case 'info':
                                echo $this->booking->getInfo();
                                break;

                        }
                        die();
                    }


                    $id = $this->booking->FindId($trans_id,$type->type);
                    if(!$id){
                        echo $this->booking->getFlag().":";

                        switch($this->booking->getFlag()){
                            case 'error':
                                echo $this->booking->getError();
                                break;
                            case 'numrows':
                                echo $this->booking->getNumrows();
                                break;
                            case 'info':
                                echo $this->booking->getInfo();
                                break;

                        }
                        die();
                    }

                    /*echo $id[0]->id;
                    echo $type->type;*/

                    //return num of affected rows, if page is reload, return 0
                    //no need to test return result
                    $update = $this->booking->UpdateStatus($type->type,$id[0]->id);                        

                    //Send email to client  
                    $this->load->model('invoicem','invoicem');
                    $this->invoicem->prepare_and_send($type->type, $id[0]->id);

                    // Get data and load view with error handled

                    switch ($type->type) {
                        case 'carbooking':
                            $this->show_order_rentacar($id[0]->id);
                            break; 

                        case 'excursionbooking':
                            $this->show_order_exc();
                            break;

                        case 'tourbooking':
                            $this->show_order_exc();
                            break;
                    }

                    break;            

                    case 'DECLINED':                    

                        $data['title'] = 'DECLINED';
                        $data['layout_id'] = 'layout2';
                        $this->core_site('order/result_declined', $data);
                        break;

                    case 'FAILED':

                        $data['title'] = 'FAILED';
                        $data['layout_id'] = 'layout2';
                        $this->core_site('order/result_declined', $data);
                        break;
                }     


            }

        }

        function show_order_rentacar($id) {

            $dbRet = $this->booking->ShowOrder($id);

            //print_r($dbRet);
            $order = $this->booking->test_response($dbRet);
            if(!$order){
                echo $this->booking->getFlag().":";

                switch($this->booking->getFlag()){
                    case 'error':
                        echo $this->booking->getError();
                        break;
                    case 'numrows':
                        echo $this->booking->getNumrows();
                        break;
                    case 'info':
                        echo $this->booking->getInfo();
                        break;

                }
                die();
            }

            $order = $this->booking->test_response($dbRet[0]);

            if($order){
                //print_r($order); 
                $data['order'] = $order;

                $order_extras = $this->booking->test_response($dbRet[1]);

                if($order_extras){
                    //print_r($order_extras);
                    $data['order_extras'] = $order_extras;
                }else{
                    //echo 'Accessories '.$this->booking->getFlag().":";

                    switch($this->booking->getFlag()){
                        case 'error':
                            echo $this->booking->getError();
                            break;
                        case 'numrows':
                            //echo $this->booking->getNumrows();
                            break;
                        case 'info':
                            echo $this->booking->getInfo();
                            break;

                    }
                }

                $data['title'] = 'Order OK';
                $data['layout_id'] = 'layout2';
                $this->core_site('order/result_ok', $data);

            }else{
                echo $this->booking->getFlag().":";

                switch($this->booking->getFlag()){
                    case 'error':
                        echo $this->booking->getError();
                        break;
                    case 'numrows':
                        echo $this->booking->getNumrows();
                        break;
                    case 'info':
                        echo $this->booking->getInfo();
                        break;

                }
            }
        }


        function show_order_exc() {

            $data['title'] = 'Order OK';
            $data['layout_id'] = 'layout2';
            
            $invocie_res = $this->invoicem->get_data();
            
            $result = array_merge((array)$data, (array)$invocie_res);
            
            //print_r($result);
            $this->core_site('order/result_ok_exc', $result);
        }
    }

?>
